﻿
using GCC.Container.Abstracts;
using GCC.JobContainer.WSModels;

using WSClient = System.IMServer.Client.Manager;
namespace GCC.APIContainer
{
    /// <summary>
    /// 容器上下文
    /// </summary>
    public partial class GCCContext : AbsGCCContext
    {

        /// <summary>
        /// 获取所有的在线用户ID
        /// </summary>
        /// <returns></returns>
        public override List<long> GetAllUserID()
        {
            var res = new List<long>();
            var imRes = WSClient.GetAllUser(EventID, new GCCGrpc.IMServer.GetAllUserRequest() { });
            foreach (var item in imRes.Users)
            {
                res.Add(item.UID);
            }

            return res;
        }

        /// <summary>
        /// 給某个用户发送文本的消息
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <param name="text">文本</param>
        public override void SendTextMessageToUser(long uid, string text)
        {
            _ = WSClient.SendMessageToUser(EventID, new GCCGrpc.IMServer.SendMessageToUserRequest()
            {
                UID = uid,
                Text = text
            });
        }

        /// <summary>
        /// 给所有在线的人光波
        /// </summary>
        /// <param name="text"></param>
        public override void BoardCastTextMessage(string text)
        {
            _ = WSClient.SendMessageToAllUser(EventID, new GCCGrpc.IMServer.SendMessageToAllUserRequest()
            {
                Text = text
            });
        }

        /// <summary>
        /// 获取用户的文本websocket客户端链接
        /// </summary> 
        /// <param name="uid">用户ID</param>
        /// <returns></returns> 
        public override List<AbsWebsocketClient> GetUserWSClient<T>(long uid)
        {
            var res = new List<AbsWebsocketClient>();
            var imRes = WSClient.GetAllUser(EventID, new GCCGrpc.IMServer.GetAllUserRequest());
            foreach (var item in imRes.Users)
            {
                res.Add(new WSClientItem()
                {
                    ID = item.ClientID,
                    Type = item.ClientType
                });
            }

            return res;

        }

        /// <summary>
        /// 强制某个类型的端下线
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <param name="clientID">端ID不传就是全部</param>
        public override void ForcedOffline<T>(long uid, string? clientID)
        {
            if (typeof(T) == typeof(string))
            {
                _ = WSClient.ForcedOffline(EventID, new GCCGrpc.IMServer.ForcedOfflineRequest()
                {
                    ClientID = clientID,
                    UID = uid
                });
            }
        }

        /// <summary>
        /// 給某个用户的某个端发送文本的消息
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <param name="clientID">用户clientID</param>
        /// <param name="text">文本</param>
        public override void SendTextMessageToUserClient(long uid, string clientID, string text)
        {
            _ = WSClient.SendMessageToUserClient(EventID, new GCCGrpc.IMServer.SendMessageToUserClientRequest()
            {
                ClientID = clientID,
                UID = uid,
                Text = text
            });
        }

    }
}
